
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/assumptions/ask.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:26:00 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Ask &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="ask.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Assume" href="assume.html" />
    <link rel="prev" title="Assumptions" href="index.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="assume.html" title="Assume"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Assumptions"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Assumptions</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Ask</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="module-sympy.assumptions.ask">
<span id="ask"></span><h1>Ask<a class="headerlink" href="#module-sympy.assumptions.ask" title="Permalink to this headline">¶</a></h1>
<p>Module for querying SymPy objects about assumptions.</p>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.assumptions.ask.AssumptionKeys">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.assumptions.ask.</span></span><span class="sig-name descname"><span class="pre">AssumptionKeys</span></span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/ask.py#L19-L303"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.assumptions.ask.AssumptionKeys" title="Permalink to this definition">¶</a></dt>
<dd><p>This class contains all the supported keys by <code class="docutils literal notranslate"><span class="pre">ask</span></code>.
It should be accessed via the instance <code class="docutils literal notranslate"><span class="pre">sympy.Q</span></code>.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.assumptions.ask.ask">
<span class="sig-prename descclassname"><span class="pre">sympy.assumptions.ask.</span></span><span class="sig-name descname"><span class="pre">ask</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">proposition</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">assumptions</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/ask.py#L356-L494"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.assumptions.ask.ask" title="Permalink to this definition">¶</a></dt>
<dd><p>Function to evaluate the proposition with assumptions.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>proposition</strong> : Any boolean expression.</p>
<blockquote>
<div><p>Proposition which will be evaluated to boolean value. If this is
not <code class="docutils literal notranslate"><span class="pre">AppliedPredicate</span></code>, it will be wrapped by <code class="docutils literal notranslate"><span class="pre">Q.is_true</span></code>.</p>
</div></blockquote>
<p><strong>assumptions</strong> : Any boolean expression, optional.</p>
<blockquote>
<div><p>Local assumptions to evaluate the <em>proposition</em>.</p>
</div></blockquote>
<p><strong>context</strong> : AssumptionsContext, optional.</p>
<blockquote>
<div><p>Default assumptions to evaluate the <em>proposition</em>. By default,
this is <code class="docutils literal notranslate"><span class="pre">sympy.assumptions.global_assumptions</span></code> variable.</p>
</div></blockquote>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code>, or <code class="docutils literal notranslate"><span class="pre">None</span></code></p>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><p><strong>TypeError</strong> : <em>proposition</em> or <em>assumptions</em> is not valid logical expression.</p>
<p><strong>ValueError</strong> : assumptions are inconsistent.</p>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>This function evaluates the proposition to <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code> if
the truth value can be determined. If not, it returns <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<p>It should be discerned from <a class="reference internal" href="refine.html#sympy.assumptions.refine.refine" title="sympy.assumptions.refine.refine"><code class="xref py py-func docutils literal notranslate"><span class="pre">refine()</span></code></a> which, when applied to a
proposition, simplifies the argument to symbolic <code class="docutils literal notranslate"><span class="pre">Boolean</span></code> instead of
Python built-in <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code> or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<p><strong>Syntax</strong></p>
<blockquote>
<div><ul class="simple">
<li><dl class="simple">
<dt>ask(proposition)</dt><dd><p>Evaluate the <em>proposition</em> in global assumption context.</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>ask(proposition, assumptions)</dt><dd><p>Evaluate the <em>proposition</em> with respect to <em>assumptions</em> in
global assumption context.</p>
</dd>
</dl>
</li>
</ul>
</div></blockquote>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">ask</span><span class="p">,</span> <span class="n">Q</span><span class="p">,</span> <span class="n">pi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">rational</span><span class="p">(</span><span class="n">pi</span><span class="p">))</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">even</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">),</span> <span class="n">Q</span><span class="o">.</span><span class="n">even</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">prime</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="p">),</span> <span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">False</span>
</pre></div>
</div>
<p>If the truth value cannot be determined, <code class="docutils literal notranslate"><span class="pre">None</span></code> will be returned.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">odd</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="p">)))</span> <span class="c1"># cannot determine unless we know x</span>
<span class="go">None</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">ValueError</span></code> is raised if assumptions are inconsistent.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">Q</span><span class="o">.</span><span class="n">even</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">Q</span><span class="o">.</span><span class="n">odd</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">inconsistent assumptions Q.even(x) &amp; Q.odd(x)</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>Relations in assumptions are not implemented (yet), so the following
will not give a meaningful result.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">positive</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>It is however a work in progress.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="refine.html#sympy.assumptions.refine.refine" title="sympy.assumptions.refine.refine"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.assumptions.refine.refine</span></code></a></dt><dd><p>Simplification using assumptions. Proposition is not reduced to <code class="docutils literal notranslate"><span class="pre">None</span></code> if the truth value cannot be determined.</p>
</dd>
</dl>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.assumptions.ask.register_handler">
<span class="sig-prename descclassname"><span class="pre">sympy.assumptions.ask.</span></span><span class="sig-name descname"><span class="pre">register_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">handler</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/ask.py#L580-L601"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.assumptions.ask.register_handler" title="Permalink to this definition">¶</a></dt>
<dd><p>Register a handler in the ask system. key must be a string and handler a
class inheriting from AskHandler.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 1.8.: </span>Use multipledispatch handler instead. See <a class="reference internal" href="assume.html#sympy.assumptions.assume.Predicate" title="sympy.assumptions.assume.Predicate"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Predicate</span></code></a>.</p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.assumptions.ask.remove_handler">
<span class="sig-prename descclassname"><span class="pre">sympy.assumptions.ask.</span></span><span class="sig-name descname"><span class="pre">remove_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">handler</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/ask.py#L604-L620"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.assumptions.ask.remove_handler" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes a handler from the ask system. Same syntax as register_handler</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 1.8.: </span>Use multipledispatch handler instead. See <a class="reference internal" href="assume.html#sympy.assumptions.assume.Predicate" title="sympy.assumptions.assume.Predicate"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Predicate</span></code></a>.</p>
</div>
</dd></dl>

</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">Assumptions</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="assume.html"
                        title="next chapter">Assume</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/assumptions/ask.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="assume.html" title="Assume"
             >next</a> |</li>
        <li class="right" >
          <a href="index.html" title="Assumptions"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Assumptions</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Ask</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/assumptions/ask.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:26:00 GMT -->
</html>